package com.news.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.news.entity.News;
import com.news.util.JDBCUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
import javafx.beans.binding.StringBinding;

/**
 * 新闻信息分页类
 * @author 余弓
 *
 */
public class NewsPageDao {
	public int pageSize=5;
	public int getTotal(){
		JDBCUtil util =JDBCUtil.getInstance();
		String sql ="select count(1) as total from tb_news";
		ResultSet rs =util.query(sql);
		try {
			if(rs.next()){
				int total =rs.getInt("total");
				return total;
				
			}
			return 0;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
			
		}finally {
			util.close();
		}
	}
	public List<News> getNewsByCurrentPage(int current){
		JDBCUtil util =JDBCUtil.getInstance();
		try {
			String sql="select * from tb_news order by id DESC limit ?,?";
			List<Object> params =new ArrayList<Object>();
			params.add((current-1)*pageSize);
			params.add(pageSize);
			ResultSet rs =util.query(sql,params);
			List<News> news =new ArrayList<News>();
			while(rs.next()){
				int id=rs.getInt("id");
				String title=rs.getString("title");
				String content=rs.getString("content");
				String image_url=rs.getString("image_url");
				String news_type=rs.getString("news_type");
				String user_id=rs.getString("user_id");
				News news1 =new News();
				news1.setId(id);
				news1.setTitle(title);
				news1.setContent(content);
				news1.setImage_url(image_url);
				news1.setNews_type(news_type);
				news1.setUser_id(Integer.parseInt(user_id));
				news.add(news1);
			}
			System.out.println(news);
			return news;
		} catch (Exception e) {
			return null;
		}finally {
			util.close();
		}
	}
}
